package com.xingzhe.content.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingzhe.base.exception.XingZhePlusException;
import com.xingzhe.content.mapper.TeachplanMapper;
import com.xingzhe.content.mapper.TeachplanMediaMapper;
import com.xingzhe.content.model.dto.BindTeachplanMediaDto;
import com.xingzhe.content.model.dto.SaveTeachPlanDto;
import com.xingzhe.content.model.dto.TeachplanDto;
import com.xingzhe.content.model.po.Teachplan;
import com.xingzhe.content.model.po.TeachplanMedia;
import com.xingzhe.content.service.TeachPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class TeachPlanServiceImpl implements TeachPlanService {
    @Resource
    private TeachplanMapper teachplanMapper;

    @Resource
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachPlanTree(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    private int getTeachPlanCount(Long courseId,Long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId).eq(Teachplan::getParentid,parentId);
        return teachplanMapper.selectCount(queryWrapper);
    }

    @Transactional
    @Override
    public void saveTeachPlan(SaveTeachPlanDto teachPlanDto) {
        //通过课程计划id判断是新增还是修改 因为新增一开始是没有id的  修改是已经有id了
        Long id = teachPlanDto.getId();
        if (id==null){
            //新增
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachPlanDto,teachplan);
            //确认排序的字段
            Long courseId = teachPlanDto.getCourseId();
            Long parentId = teachPlanDto.getParentid();
            //获取排序的数字
            int count = getTeachPlanCount(courseId, parentId);
            teachplan.setOrderby(count+1);
            //插入到数据库
            teachplanMapper.insert(teachplan);
        }else {
            //修改
            Teachplan teachplan = teachplanMapper.selectById(id);
            //拷贝覆盖
            BeanUtils.copyProperties(teachPlanDto,teachplan);
            //更新
            teachplanMapper.updateById(teachplan);
        }
    }

    @Override
    public void deleteTeachPlan(Long id) {
        //删除第一级别的大章节时要求大章节下边没有小章节时方可删除。
        //删除第二级别的小章节的同时需要将teachplan_media表关联的信息也删除。
        //1.查询是删除大章节还是小章节
        Teachplan teachplan = teachplanMapper.selectById(id);
        //grade为1是大章节，为2是小章节
        Integer grade = teachplan.getGrade();
        if (grade==1){
            //查看大章节下面有没有小章节
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            //设置查询条件为查询子章节的父章节id是否与当前大章节id相等
            queryWrapper.eq(Teachplan::getParentid,teachplan.getId());
            Integer count = teachplanMapper.selectCount(queryWrapper);
            if (count>0){
                //有附属小章节，则提示
                XingZhePlusException.cast("当前大章节有小章节，请先删除小章节！");
                return;
            }
                //没有附属小章节，直接删
            teachplanMapper.deleteById(id);
            return;
        }
        //删除小章节同时要删除它附带的媒资
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId,id);
        teachplanMediaMapper.delete(queryWrapper);
        teachplanMapper.deleteById(id);
    }

    @Transactional
    @Override
    public List<TeachplanDto> moveDownTeachPlan(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        Integer grade = teachplan.getGrade();
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        //如果是大章节
        if (grade==1){
            queryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId())
                    .eq(Teachplan::getParentid,0);
            Integer count = teachplanMapper.selectCount(queryWrapper);
            //如果已经在最下面
            if (Objects.equals(teachplan.getOrderby(), count)){
                XingZhePlusException.cast("已经在最下面啦！");
                return findTeachPlanTree(teachplan.getCourseId());
            }
            //还不在最下面，还可以往下移动
                LambdaQueryWrapper<Teachplan> queryWrapperSon = new LambdaQueryWrapper<>();
                queryWrapperSon.eq(Teachplan::getCourseId,teachplan.getCourseId())
                                .eq(Teachplan::getOrderby,teachplan.getOrderby()+1)
                                        .eq(Teachplan::getParentid,0);
                Teachplan teachplan1 = teachplanMapper.selectOne(queryWrapperSon);
                teachplan1.setOrderby(teachplan1.getOrderby()-1);
                teachplanMapper.updateById(teachplan1);
                teachplan.setOrderby(teachplan.getOrderby()+1);
                teachplanMapper.updateById(teachplan);
                return findTeachPlanTree(teachplan.getCourseId());
        }
        //如果是小章节
        queryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId())
                .eq(Teachplan::getParentid,teachplan.getParentid())
                .eq(Teachplan::getOrderby,teachplan.getOrderby()+1);
        //查询总共有多少小章节，然后根据当前要改的小章节位置判断是否与总章节数相等，相等则证明已经在最下面了
        LambdaQueryWrapper<Teachplan> queryWrapperCount = new LambdaQueryWrapper<>();
        queryWrapperCount.eq(Teachplan::getCourseId,teachplan.getCourseId())
                .eq(Teachplan::getParentid,teachplan.getParentid());
        Integer count = teachplanMapper.selectCount(queryWrapperCount);
        if (Objects.equals(count, teachplan.getOrderby())){
            XingZhePlusException.cast("已经在最下面啦！");
            return findTeachPlanTree(teachplan.getCourseId());
        }
        Teachplan teachplan1 = teachplanMapper.selectOne(queryWrapper);
        teachplan1.setOrderby(teachplan1.getOrderby()-1);
        teachplan.setOrderby(teachplan.getOrderby()+1);
        teachplanMapper.updateById(teachplan1);
        teachplanMapper.updateById(teachplan);
        return findTeachPlanTree(teachplan.getCourseId());
    }

    @Transactional
    @Override
    public List<TeachplanDto> moveUpTeachPlan(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        Integer order = teachplan.getOrderby();
        if (order==1){
            XingZhePlusException.cast("已经在最上边了哦~");
            return findTeachPlanTree(teachplan.getCourseId());
        }
        Integer grade = teachplan.getGrade();

        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        //如果是大章节
        if (grade==1){
            queryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId())
                    .eq(Teachplan::getParentid,0)
                    .eq(Teachplan::getOrderby,order-1);
            Teachplan teachplan1 = teachplanMapper.selectOne(queryWrapper);
            teachplan1.setOrderby(teachplan1.getOrderby()+1);
            teachplanMapper.updateById(teachplan1);
        }else {
            //如果是小章节
            queryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId())
                    .eq(Teachplan::getParentid,teachplan.getParentid())
                    .eq(Teachplan::getOrderby,order-1);
            Teachplan teachplan1 = teachplanMapper.selectOne(queryWrapper);
            teachplan1.setOrderby(teachplan1.getOrderby()+1);
            teachplanMapper.updateById(teachplan1);
        }
        //最后移动
        teachplan.setOrderby(order-1);
        teachplanMapper.updateById(teachplan);
        return findTeachPlanTree(teachplan.getCourseId());
    }

    @Transactional
    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //课程计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if (teachplan==null){
            XingZhePlusException.cast("课程计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            XingZhePlusException.cast("只允许第二级教学计划绑定媒资文件");
        }


        //先删除原有的记录，再根据课程计划id删除它所绑定的媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId,bindTeachplanMediaDto.getTeachplanId()));

        //然后添加新记录
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto,teachplanMedia);
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMediaMapper.insert(teachplanMedia);
    }

    @Override
    public void NotAssociationMedia(Long teachPlanId, String mediaId) {
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getMediaId,mediaId)
                .eq(TeachplanMedia::getTeachplanId,teachPlanId));
    }

}
